package com.zbs.framework.core.function.action;

import com.alibaba.fastjson.JSONArray;
import com.zbs.framework.common.utils.ResultData;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.dao.function.entity.Function;
import com.zbs.framework.dao.function.service.FunctionService;
import com.zbs.framework.dao.function.service.model.FunctionNode;
import com.zbs.framework.dao.function.view.FunctionNodeView;
import com.zbs.framework.dao.function.view.FunctionView;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiParam;
import com.zbs.framework.swagger.annotations.ApiTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能控制器
 * Create by Zbs on 2019/07/08.
 */
@ApiTags(value = "功能管理",group = "UIM")
@RestController
@RequestMapping("/function")
public class FunctionController {

    @Autowired
    private FunctionService functionService;

    /**
     * 添加功能点
     * @param parentId
     * @param identifier
     * @param name
     * @param description
     * @return
     */
    @ApiOperation(value = "添加功能点",response = FunctionView.class)
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys::addFunction')")
    public ResultData<Function> add(@ApiParam("功能父节点id") Long parentId,
                          @ApiParam(value = "功能唯一标识",required = true) String identifier,
                          @ApiParam(value = "功能名称",required = true) String name,
                          @ApiParam("功能描述") String description,
                          @ApiParam("功能关联权限id") String permIds) {
        List<Long> permissionIds = JSONArray.parseArray(permIds, Long.class);
        Function function = functionService.save(parentId, identifier, name,
                description, permissionIds);
        //返回数据
        return ResultUtil.ok(function);
    }

    /**
     * 编辑功能点
     *
     * @param id
     * @param identifier
     * @param name
     * @param description
     * @param permIds
     * @return
     */
    @ApiOperation("编辑功能点")
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys::editFunction')")
    public ResultData<?> edit(@ApiParam(value = "功能ID",required = true) Long id,
                           @ApiParam(value = "功能唯一标识",required = true) String identifier,
                           @ApiParam(value = "功能名称",required = true) String name,
                           @ApiParam("功能描述") String description,
                           @ApiParam("功能关联权限id") String permIds) {
        List<Long> permissionIds = JSONArray.parseArray(permIds, Long.class);
        functionService.edit(id, identifier, name, description, permissionIds);
        return ResultUtil.ok();
    }

    /**
     * 删除功能点
     *
     * @param id
     * @return
     */
    @ApiOperation("删除功能点")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys::deleteFunction')")
    public ResultData<?> delete(@ApiParam(value = "功能ID",required = true) Long id) {
        functionService.delete(id);
        return ResultUtil.ok();
    }

    /**
     * 获取功能点树
     *
     * @param rootId 根功能点ID
     * @return
     */
    @ApiOperation(value = "获取功能点树",response = FunctionNodeView.class)
    @PostMapping("/getTree.do")
    public ResultData<List<FunctionNode>> findServicePermissions(@ApiParam(value = "功能节点ID") Long rootId) {
        return ResultUtil.ok(functionService.getTree(rootId));
    }

}
